# 六维力传感器CAN SDK Makefile

# 编译器和参数
CXX = g++
CXXFLAGS = -std=c++14 -Wall -Wextra -O2 -pthread
INCLUDES = 
LIBS = 

# 目录
SRCDIR = .
BUILDDIR = build
LIBDIR = lib

# 源文件
SDK_SOURCES = six_axis_sensor_can_sdk.cpp
EXAMPLE_SOURCES = can_sdk_example.cpp
SDK_HEADERS = six_axis_sensor_can_sdk.h

# 目标文件
SDK_OBJECTS = $(patsubst %.cpp, $(BUILDDIR)/%.o, $(SDK_SOURCES))
EXAMPLE_OBJECTS = $(patsubst %.cpp, $(BUILDDIR)/%.o, $(EXAMPLE_SOURCES))

# 输出文件
STATIC_LIB = $(LIBDIR)/libsix_axis_sensor_can.a
EXAMPLE_BIN = $(BUILDDIR)/can_sdk_example

# 默认目标
all: $(STATIC_LIB) $(EXAMPLE_BIN)

# 创建目录
$(BUILDDIR):
	@mkdir -p $(BUILDDIR)

$(LIBDIR):
	@mkdir -p $(LIBDIR)

# 编译SDK对象文件
$(BUILDDIR)/%.o: %.cpp $(SDK_HEADERS) | $(BUILDDIR)
	@echo "🔨 编译 $<"
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@

# 创建静态库
$(STATIC_LIB): $(SDK_OBJECTS) | $(LIBDIR)
	@echo "📦 创建静态库 $@"
	ar rcs $@ $^

# 编译示例程序
$(EXAMPLE_BIN): $(EXAMPLE_OBJECTS) $(STATIC_LIB) | $(BUILDDIR)
	@echo "🔗 链接示例程序 $@"
	$(CXX) $(CXXFLAGS) $< $(STATIC_LIB) $(LIBS) -o $@

# 安装
install: $(STATIC_LIB) $(SDK_HEADERS)
	@echo "📥 安装SDK到系统..."
	sudo mkdir -p /usr/local/include/six_axis_sensor
	sudo mkdir -p /usr/local/lib
	sudo cp $(SDK_HEADERS) /usr/local/include/six_axis_sensor/
	sudo cp $(STATIC_LIB) /usr/local/lib/
	@echo "✅ 安装完成"

# 卸载
uninstall:
	@echo "🗑️  卸载SDK..."
	sudo rm -rf /usr/local/include/six_axis_sensor
	sudo rm -f /usr/local/lib/libsix_axis_sensor_can.a
	@echo "✅ 卸载完成"

# 清理
clean:
	@echo "🧹 清理编译文件..."
	rm -rf $(BUILDDIR) $(LIBDIR)

# 测试
test: $(EXAMPLE_BIN) $(INTERACTIVE_BIN)
	@echo "🧪 可用的测试程序:"
	@echo "  1. 基础示例: sudo ./$(EXAMPLE_BIN) [接口名]"
	@echo "  2. 交互式示例: sudo ./$(INTERACTIVE_BIN) [接口名]"
	@echo "注意：需要root权限和正确配置的CAN接口"

test-run: $(EXAMPLE_BIN)
	sudo ./$(EXAMPLE_BIN) $(INTERFACE)

test-interactive: $(INTERACTIVE_BIN)
	sudo ./$(INTERACTIVE_BIN) $(INTERFACE)

# 设置CAN接口 (虚拟)
setup-vcan:
	@echo "🔧 设置虚拟CAN接口..."
	sudo modprobe can
	sudo modprobe vcan
	sudo ip link add dev vcan0 type vcan 2>/dev/null || true
	sudo ip link set up vcan0
	@echo "✅ 虚拟CAN接口vcan0已准备就绪"

# 设置真实CAN接口
setup-can:
	@echo "🔧 设置CAN接口..."
	@echo "请手动运行以下命令配置你的CAN接口:"
	@echo "sudo ip link set dev can1 down"
	@echo "sudo ip link set dev can1 up type can bitrate 1000000 dbitrate 1000000 fd on"

# 监控CAN消息
monitor:
	@echo "👀 监控CAN消息 (需要安装can-utils)..."
	@which candump >/dev/null || (echo "请安装can-utils: sudo apt install can-utils" && exit 1)
	candump $(INTERFACE)

# 帮助
help:
	@echo "六维力传感器CAN SDK编译选项:"
	@echo ""
	@echo "基本编译:"
	@echo "  make               - 编译SDK和示例程序"
	@echo "  make clean         - 清理编译文件"
	@echo ""
	@echo "安装/卸载:"
	@echo "  make install       - 安装SDK到系统(/usr/local/)"
	@echo "  make uninstall     - 从系统卸载SDK"
	@echo ""
	@echo "测试:"
	@echo "  make setup-vcan    - 设置虚拟CAN接口vcan0"
	@echo "  make test-run INTERFACE=vcan0      - 运行基础测试程序"
	@echo "  make test-interactive INTERFACE=vcan0 - 运行交互式程序(推荐)"
	@echo "  make monitor INTERFACE=vcan0       - 监控CAN消息"
	@echo ""
	@echo "快速开始:"
	@echo "  make && make setup-vcan && make test-interactive INTERFACE=vcan0"

.PHONY: all install uninstall clean test test-run test-interactive setup-vcan setup-can monitor help